package org.example.proxy;

import lombok.extern.slf4j.Slf4j;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Method;
@Slf4j
public class CGLibMethodInterceptor implements MethodInterceptor {

    private  Object target;
    public CGLibMethodInterceptor(Object target) {
        this.target=target;
    }
    /**
     * 调用目标方法, 并对目标方法进行功能增强
     * @param o  代理类
     * @param method  目标方法
     * @param objects 参数
     * @param methodProxy
     * @return
     * @throws Throwable
     */
    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        log.info("cglib代理开始");
        Object result=method.invoke(target,objects);
        log.info("cglib代理结束");
        return result;
    }
}
